package com.ikkong.school.controller;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.ikkong.common.vo.User;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.school.model.Buynum;
import com.ikkong.school.model.Suppliers;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.school.model.Buy;
import com.ikkong.school.service.BuyService;
import com.ikkong.school.service.impl.BuyServiceImpl;
import com.ikkong.system.util.CommonUtil;
import com.jfinal.aop.Clear;
import com.jfinal.upload.UploadFile;
import com.mysql.fabric.ShardIndex;
import org.apache.poi.hssf.record.RecalcIdRecord;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.shiro.authz.annotation.RequiresUser;

/**
 * Generated by Blade.
 * 2018-07-09 22:42:31
 */
public class BuyController extends UrlPermissController {
	private static String CODE = "buy";
	private static String PERFIX = "buy";
	private static String LIST_SOURCE = "Buy.list";
	private static String BASE_PATH = "/platform/buy/";
	
	BuyService service = new BuyServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
        Buynum buynum = new Buynum();
        buynum.setDate(new Date());
        buynum.setStatus(0);
        int id = Blade.create(Buynum.class).saveRtId(buynum);
        setAttr("buynum",id);
        render(BASE_PATH + "buy.html");
	}
    public void check() {
        setAttr("id",getPara("id","0"));
        setAttr("code", CODE);
        render(BASE_PATH + "check.html");
    }
    public void checkli() {
        String id = getPara("id", "0");
        List<Map> map = Blade.dao().select("Buy.checkli", Map.class, Record.create().set("id", id));
        renderJson(CommonUtil.fenye(map,getParaToInt("page"),getParaToInt("rows")));
    }
    public void bycode() {
        String code = getPara("code");
        Map map = Blade.dao().selectSingle("Product.one", Record.create().set("code", code), Map.class);
        renderJson(map);
    }

	public void pass() {
        int pass = getParaToInt("pass", 1);
        int ids = getParaToInt("id",0);
        Buynum buy = new Buynum();
        buy.setId(ids);
        buy.setStatus(pass);
        boolean temp = Blade.create(Buynum.class).update(buy);
        if (temp) {
            renderJson(success(UPDATE_SUCCESS_MSG));
        } else {
            renderJson(error(UPDATE_FAIL_MSG));
        }

	}

    public void viewcontain() {
        setAttr("id",getPara("id",""));
        setAttr("code", CODE);
        render(BASE_PATH + "buy_check.html");
    }

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "buy_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Buy buy = service.findById(id);
		setAttr("model", JsonKit.toJson(buy));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "buy_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Buy buy = service.findById(id);
		setAttr("model", JsonKit.toJson(buy));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "buy_view.html");
	}

	public void list() {
        Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

    //对帐
    public void duizhang(){
        //供应商对帐
        String count = getPara("count", "0");
        setAttr("count",count);
        String para = getPara("role", "gys");
        if(para.equals("gys")){
            String schoolid = getPara("schoolid", "");
            setAttr("schoolid",schoolid);
            setAttr("suppid", CommonUtil.getSuppid());
        }else if(para.equals("school")){//学校对帐
            String suppid = getPara("suppid", "");
            setAttr("suppid",suppid);
            setAttr("schoolid",ShiroKit.getUser().getDeptId().toString());
        }
        setAttr("code",CODE);
        render("/platform/duizhang/duizhang.html");
    }

    public void duizhangli() {
        String suppid = getPara("suppid", "0");
        String schoolid = getPara("schoolid", "0");
        List<Map> select = Blade.dao().select("Buy.duizhang", Map.class, Record.create().set("suppid", suppid).set("schoolid", schoolid));
        renderJson(CommonUtil.fenye(select,getParaToInt("page",1),getParaToInt("row",20)));
    }

    //收货
    public void shouhuo() {
        String id = getPara("id","0");
        Buy buy = service.findById(id);
        buy.setStatus(3);
        buy.setFahuo_date(new Date());
        boolean update = service.update(buy);
        if (update) {
            renderJson(success(SAVE_SUCCESS_MSG));
        } else {
            renderJson(error(SAVE_FAIL_MSG));
        }
    }

	//加入发货单
    //收货status=4
	public void fahuo() {
		String id = getPara("id","0");
        Integer status = getParaToInt("status", 3);
        Buy buy = service.findById(id);
        buy.setStatus(status);
        buy.setFahuo_date(new Date());
        boolean update = service.update(buy);
        if (update) {
            renderJson(success(SAVE_SUCCESS_MSG));
        } else {
            renderJson(error(SAVE_FAIL_MSG));
        }
    }

    //全部加入发货单
    //全部收货==status=4
    public void fahuoall() {
        String buynumid = getPara("buynum", "0");
        String status = getPara("status", "3");
        String suppid = ShiroKit.getUser().getDeptId().toString();
        boolean b = false;
        if(status.equals("4")){
            service.updateBy("shouhuo_date = CURTIME() " , "buy_num = "+ buynumid + " and school_id = "+ suppid + " and status = 3" , Boolean.class);
            b = service.updateBy("status = " + status, "buy_num = "+ buynumid + " and school_id = "+ suppid + " and status = 3" , Boolean.class);

        }else {
            Suppliers suppliers = Blade.create(Suppliers.class).findFirstBy("d_id = " + suppid, Suppliers.class);
            suppid = suppliers.getId().toString();
            service.updateBy("fahuo_date = CURTIME()  ", "buy_num = "+ buynumid + " and supp_id = "+ suppid  + " and status = 0", Boolean.class);
            b = service.updateBy("status = " + status, "buy_num = "+ buynumid + " and supp_id = "+ suppid + " and status = 0", Boolean.class);

        }

        if (b) {
            renderJson(success("操作成功"));
        } else {
            renderJson(error("操作失败"));
        }
    }

	public void save() {
        Buy bean = getBean(Buy.class);
        bean.setDate(new Date());
        bean.setStatus(0);
        bean.setSchool_id((int)ShiroKit.getUser().getDeptId());
        Integer buy_num = bean.getBuy_num();
        Buynum buynum = Blade.create(Buynum.class).findById(buy_num);
        buynum.setUid((int)ShiroKit.getUser().getId());
        boolean temp = service.save(bean);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Buy buy = mapping(PERFIX, Buy.class);
		boolean temp = service.update(buy);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void upby() {
        String id = getPara("id", "0");
        String type = getPara("type", "remarks");
        String value = getPara("value", "");
        boolean temp = false;
        Buy buy = service.findById(id);
        if(buy == null){
            renderJson(error(UPDATE_FAIL_MSG));
            return;
        }else {
            if("".equals(value)){
                renderJson(error(UPDATE_FAIL_MSG));
                return;
            }
            if(type.equals("remarks")){
                buy.setRemarks(value);
            }else if(type.equals("pdate")){
                java.sql.Date pdate = java.sql.Date.valueOf(value);
                buy.setProduct_date(pdate);
            }else if(type.equals("money")){
                BigDecimal bd=new BigDecimal(value);
                buy.setMoney(bd);
            }
           temp = service.update(buy);
        }
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
